<!-- views/inventory/components/ProductFilter.vue -->
<template>
  <el-card class="filter-card" shadow="never">
    <el-form :model="queryParams" inline>
      <el-form-item label="产品名称">
        <el-input
          v-model="queryParams.name"
          placeholder="输入产品名称"
          clearable
          style="width: 200px"
        />
      </el-form-item>
      
      <el-form-item label="SKU">
        <el-input
          v-model="queryParams.sku"
          placeholder="输入SKU"
          clearable
          style="width: 150px"
        />
      </el-form-item>
      
      <el-form-item label="分类">
        <el-cascader
          v-model="queryParams.categoryId"
          :options="categoryOptions"
          :props="cascaderProps"
          clearable
          placeholder="请选择分类"
          style="width: 200px"
        />
      </el-form-item>
      
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" clearable placeholder="全部状态" style="width: 120px">
          <el-option label="在售" value="active" />
          <el-option label="停售" value="inactive" />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleSearch">
          查询
        </el-button>
        <el-button :icon="Refresh" @click="handleReset">
          重置
        </el-button>
        <el-button 
          link
          :icon="SetUp" 
          @click="showAdvancedFilter = !showAdvancedFilter"
        >
          高级筛选
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 高级筛选 -->
    <el-collapse-transition>
      <div v-show="showAdvancedFilter" class="advanced-filter">
        <el-divider content-position="left">高级筛选</el-divider>
        <el-form :model="queryParams" inline>
          <el-form-item label="品牌">
            <el-select
              v-model="queryParams.brandId"
              placeholder="请选择品牌"
              clearable
              filterable
              style="width: 150px"
            >
              <el-option
                v-for="brand in brandOptions"
                :key="brand?.id || ''"
                :label="brand?.name || ''"
                :value="brand?.id || ''"
              />
            </el-select>
          </el-form-item>
          
          <el-form-item label="规格">
            <el-input
              v-model="queryParams.specification"
              placeholder="输入规格"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          
          <el-form-item label="最小库存">
            <el-input-number
              v-model="queryParams.minStock"
              :min="0"
              placeholder="最小库存"
              style="width: 120px"
            />
          </el-form-item>
          
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="queryParams.createDateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
  </el-card>
</template>

<script setup lang="ts">
// @ts-nocheck
import { ref, reactive, defineProps, defineEmits, onMounted } from 'vue'
import { Search, Refresh, SetUp } from '@element-plus/icons-vue'
import * as inventoryAPI from '@/api/inventory'

const showAdvancedFilter = ref(false)
const categoryOptions = ref<any[]>([])
const brandOptions = ref<inventoryAPI.Brand[]>([])

// 定义查询参数接口
interface QueryParams {
  name: string
  sku: string
  categoryId: number | null
  status: string
  brandId: number | null
  specification: string
  minStock: number | undefined
  createDateRange: string[]
  page: number
  limit: number
}

const props = defineProps<{
  queryParams: QueryParams
}>()

const emit = defineEmits<{
  (e: 'search'): void
  (e: 'reset'): void
}>()

// 级联选择器配置
const cascaderProps = {
  value: 'id',
  label: 'name',
  children: 'children',
  expandTrigger: 'hover' as const,
  multiple: false,
  // 只返回选中节点的值，而不是整个路径
  emitPath: false
}

// 加载类目数据
const loadCategories = async () => {
  try {
    const response = await inventoryAPI.getProductCategories()
    categoryOptions.value = response.data || []
  } catch (error) {
    console.error('加载产品类目失败:', error)
  }
}

// 加载品牌数据
const loadBrands = async () => {
  try {
    const response = await inventoryAPI.getBrands()
    brandOptions.value = response.data || []
  } catch (error) {
    console.error('加载品牌数据失败:', error)
  }
}

const handleSearch = () => {
  emit('search')
}

const handleReset = () => {
  emit('reset')
}

// 组件挂载时加载类目和品牌数据
onMounted(() => {
  loadCategories()
  loadBrands()
})
</script>

<style scoped>
.filter-card {
  margin-bottom: 20px;
}

.advanced-filter {
  margin-top: 16px;
}

.advanced-filter :deep(.el-divider) {
  margin: 16px 0;
}
</style>